/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017, 2024 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "flash_def.h"
#include "lfs.h"
/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */

 
uint8_t dataBuffer[2048] = {0}; /*一个page 2112个字节，前2048个是数据*/

SDK_ALIGN(static uint8_t s_nand_program_buffer[4096], 4);

extern status_t flexspi_nor_flash_read(FLEXSPI_Type *base, uint32_t dstAddr, const uint32_t *src, uint32_t length);
extern unsigned short _acblack_cat[];
#define TEST_BUFFER_IN_FLASH _acblack_cat

static uint8_t read_buf[4096];
static uint8_t write_buf[4096];
static uint8_t lookahead_buf[4096];

int lfs_read(const struct lfs_config *c, lfs_block_t block,lfs_off_t off, void *buffer, lfs_size_t size);
int lfs_prog(const struct lfs_config *c, lfs_block_t block,lfs_off_t off, const void *buffer, lfs_size_t size);
int lfs_erase(const struct lfs_config *c, lfs_block_t block);
int lfs_sync(const struct lfs_config *c);

lfs_t lfs_gd5f1gm7;

lfs_file_t file;

static struct lfs_config lfs_5F1GMxx_cfg = 
{
	.read = lfs_read,
	.prog = lfs_prog,
	.erase = lfs_erase,
	.sync = lfs_sync,
	
	.read_size = 16,//最小读取单位为2048字节
	.prog_size = 16,//最小编程单位为2048个字节
	.block_size = 512,//一块page为2048个字节
	.block_count = 2,//总共有1024个block,1024个block为
	.block_cycles = 500,//
	.cache_size = 16,//读写缓存为4096字节
	.lookahead_size = 16,
//	
//	.read_buffer = read_buf,
//	.prog_buffer = write_buf,
//	.lookahead_buffer = lookahead_buf,
    
};
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
        const void *buffer, lfs_size_t size);

char buffer[] = "yf is good\r\n";
char buffer_read[20] = {};
int main(void)
{
    char ch;
    uint8_t regStatus = 0;
    status_t status;
    
    uint32_t block = 0;
    uint32_t page  = 0;
     
     
    /* Init board hardware. */
    BOARD_ConfigMPU();
    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    /* Just enable the trace clock, leave coresight initialization to IDE debugger */
    SystemCoreClockUpdate();
    CLOCK_EnableClock(kCLOCK_Trace);
    int err = lfs_mount(&lfs_gd5f1gm7,&lfs_5F1GMxx_cfg);
    err = lfs_format(&lfs_gd5f1gm7, &lfs_5F1GMxx_cfg);
    
    err = lfs_file_open(&lfs_gd5f1gm7, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
    
//    lfs_file_write(&lfs_gd5f1gm7,&file,buffer,sizeof(buffer));
    
    lfs_file_read(&lfs_gd5f1gm7,&file,buffer_read,sizeof(buffer));
//    flexspi_flash_port_init(EXAMPLE_FLEXSPI,NAND_FLASH_PORT);
//    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xC0, &regStatus);
//    
//    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xA0, &regStatus);
//    status = flexspi_nand_set_status(EXAMPLE_FLEXSPI, 0xA0, 0);
//    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xA0, &regStatus);
    PRINTF("hello\r\n");
    while(1){

    }
}
